﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.AdminCP.Common;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace LinkCard.AdminCP.Deal
{
    public partial class deal_all_pending : System.Web.UI.Page
    {
        public string sFullname = "";
        public string sCardNumber = "";
        public string sBirthday = "";
        public string sID = "";
        public string sMobilePhone = "";

        private int _totalRecords;

        private int nPageSize = 20;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            else
            {
                if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "SYSADMIN") == false)
                {
                    if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "HOMEPAGE") == false)
                    {
                        Response.Redirect("/message_box.aspx?why=1&msg=5");
                    }
                }
            }

            if (!Page.IsPostBack)
            {
                if (Request.QueryString["Fullname"] != null) { sFullname = Request.QueryString["Fullname"].Trim(); }
                if (Request.QueryString["CardNumber"] != null) { sCardNumber = Request.QueryString["CardNumber"].Trim(); }
                if (Request.QueryString["Birthday"] != null) { sBirthday = Request.QueryString["Birthday"].Trim(); }
                if (Request.QueryString["ID"] != null) { sID = Request.QueryString["ID"].Trim(); }
                if (Request.QueryString["MobilePhone"] != null) { sMobilePhone = Request.QueryString["MobilePhone"].Trim(); }


                DataBindPaging();
            }

        }

        private void DataBindPaging()
        {

            try
            {
                string sFilter = "AND DealTemp.Status = 1";
                if (sFullname != "" && sFullname != "Họ và tên") sFilter = sFilter + " AND FullName like N'%" + sFullname + "%'";

                if (sCardNumber != "" && sCardNumber != "Mã thẻ") sFilter = sFilter + " AND CardNumber = '" + sCardNumber + "'";

                if (sBirthday != "" && sBirthday != "Ngày sinh") sFilter = sFilter + " AND Birthday = '" + ConvertUtility.ToShortDateTime(sBirthday).ToString() + "'";

                if (sID != "" && sID != "Số CMTND") sFilter = sFilter + " AND ID = '" + sID + "'";

                if (sMobilePhone != "" && sMobilePhone != "Điện thoại") sFilter = sFilter + " AND MobilePhone = '" + sMobilePhone + "'";


                int curpage = 0;

                if (Request.QueryString["page"] != null)
                {
                    curpage = Convert.ToInt32(Request.QueryString["page"]);
                }
                else
                {
                    curpage = 1;
                }

                DataTable dt = DataHelper.LoadPageInSQL("DealTempList", curpage, nPageSize, ref _totalRecords, new SqlParameter("@Filter", sFilter));

                if (dt.Rows.Count > 0)
                {
                    int nPageCount;

                    if (_totalRecords % nPageSize == 0)
                    {
                        nPageCount = _totalRecords / nPageSize;
                    }
                    else
                    {
                        nPageCount = Convert.ToInt32(_totalRecords / nPageSize) + 1;
                    }

                    if (nPageCount < 2) divPager.Visible = false;

                    PagerShow.Text = hash1way.ShowPageNavigation(curpage, nPageCount);

                    msgList.DataSource = dt;

                    msgList.DataBind();
                    msgNoData.Visible = false;

                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void msgList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex < 0) return;
            DataRowView row = (DataRowView)e.Item.DataItem;

            var chkDelete = (HtmlInputCheckBox)e.Item.FindControl("chkDelete");
            chkDelete.Value = row["DealID"].ToString();

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int nAction = int.Parse(ActionOption.Value);
            if (nAction == 1)
            {
                string sArray = "(0";

                foreach (RepeaterItem item in msgList.Items)
                {
                    var chkDelete = (HtmlInputCheckBox)item.FindControl("chkDelete");
                    if (chkDelete.Checked)
                    {
                        sArray = sArray + "," + chkDelete.Value + "";
                    }
                }
                sArray = sArray + ")";


                string sSQL = "delete deal where [Status] = 1 and DealID in" + sArray;
                DataHelper.ExecuteNonQuery(sSQL, true);
                
                string sSQLTemp = "delete dealtemp where DealID in" + sArray;
                DataHelper.ExecuteNonQuery(sSQLTemp, true);

                Response.Redirect("/Deal/deal_all_pending.aspx");
            }

            else if (nAction == 2)
            {
                // Update Customer
                //foreach (RepeaterItem item in msgList.Items)
                //{
                //    string sSQLUpdate = "";
                //    var chkDelete = (HtmlInputCheckBox)item.FindControl("chkDelete");
                //    if (chkDelete.Checked)
                //    {
                //        sSQLUpdate = "Update Customer set [Status] = 2, [Version] = CAST( (CAST([Version] AS int) + 1) AS nvarchar(1)) where CustomerID =" +chkDelete.Value ;
                //        DataHelper.ExecuteNonQuery(sSQLUpdate, true);
                //    }
                //}
                

                //// Delete Temp
                //string sArray = "(0";
                
                //foreach (RepeaterItem item in msgList.Items)
                //{
                //    var chkDelete = (HtmlInputCheckBox)item.FindControl("chkDelete");
                //    if (chkDelete.Checked)
                //    {
                //        sArray = sArray + "," + chkDelete.Value + "";
                //    }
                //}
                //sArray = sArray + ")";

                //string sSQL = "delete customertemp where CustomerID in" + sArray;
                //DataHelper.ExecuteNonQuery(sSQL, true);
                ////Response.Redirect("../error_info.aspx?err=1");
            }
            
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

            sFullname = Fullname.Value.Trim();
            sCardNumber = CardNumber.Value.Trim();
            sID = ID.Value.Trim();
            sMobilePhone = MobilePhone.Value.Trim();
            sBirthday = Birthday.Value.Trim();
            DataBindPaging();

        }

    }
}